<template>
  <div id="loginContainer">
    <div id="loginContent">
      <h2>elm管理后台</h2>
      <el-form label-width="120px" :rules="rules" ref="loginForm" :model="user" status-icon>
        <el-form-item label="账号" prop="loginName">
          <el-input type="text" placeholder="账号/邮箱" v-model="user.loginName" @keyup.enter.native="doLogin"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="loginPwd">
          <el-input type="password" placeholder="请输入密码" v-model="user.loginPwd"
                    @keyup.enter.native="doLogin"></el-input>
        </el-form-item>
        <el-form-item label="验证码" style="width: 320px">
          <el-input type="text" placeholder="请输入右侧验证码" v-model="user.verifyCode" disabled></el-input>
        </el-form-item>
        <el-form-item>
          <el-image style="margin-right: 0; flex: auto; margin-top: -70px; margin-left: 250px; height: 35px"
                    :src="require('../assets/verify.jpg')"/>
        </el-form-item>
        <el-form-item>
          <el-row id="btn" :gutter="100">
            <el-col :span="8">
              <el-button type="primary" plain round @click="doLogin">登陆</el-button>
            </el-col>
            <el-col :span="8">
              <el-button type="warning" plain round @click="doRegister">注册</el-button>
            </el-col>
            <el-col :span="8">
              <el-link @click="doResetPwd" type="danger">忘记密码</el-link>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </div>

    <el-dialog v-model="regDialogFormVisible" title="注册" width="500px" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :model="regForm" ref="regForm" :rules="regRules" status-icon>
        <el-form-item label="用户名" label-width="100px" prop="userName">
          <el-input v-model="regForm.userName" autocomplete="off" placeholder="输入用户名" style="width: 300px;"/>
        </el-form-item>
        <el-form-item label="密码" label-width="100px" prop="userPwd">
          <el-input v-model="regForm.userPwd" type="password" autocomplete="off" placeholder="请输入密码" style="width: 300px;"/>
        </el-form-item>
        <el-form-item label="确认密码" label-width="100px" prop="checkPwd">
          <el-input v-model="regForm.checkPwd" type="password" autocomplete="off" placeholder="再次输入密码" style="width: 300px;"/>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="regDialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submitReg">提交</el-button>
      </span>
      </template>
    </el-dialog>

    <el-dialog v-model="forgetDialogFormVisible" title="忘记密码" width="500px" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :model="regForm" ref="regForm" :rules="regRules" status-icon>
        <el-form-item label="用户名" label-width="100px" prop="userName">
          <el-input v-model="regForm.userName" autocomplete="off" placeholder="输入用户名" style="width: 300px;"/>
        </el-form-item>
        <el-form-item label="新密码" label-width="100px" prop="userPwd">
          <el-input v-model="regForm.userPwd" type="password" autocomplete="off" placeholder="请输入新密码" style="width: 300px;"/>
        </el-form-item>
        <el-form-item label="确认密码" label-width="100px" prop="checkPwd">
          <el-input v-model="regForm.checkPwd" type="password" autocomplete="off" placeholder="再次输入密码" style="width: 300px;"/>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="forgetDialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="forForget">提交</el-button>
      </span>
      </template>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "LoginView",
  data() {
    const validatePwd = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.regForm.userPwd) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      user: {
        loginName: '',
        loginPwd: '',
        verifyCode: 'jyr0'
      },
      regDialogFormVisible: false,
      forgetDialogFormVisible: false,
      formLabelWidth: '140px',
      regForm: {
        userName: '',
        userPwd: '',
        checkPwd: ''
      },
      rules: {
        loginName: [
          {required: true, message: '用户名不能为空', trigger: 'blur'},
          { min: 3, max: 16, message: '用户名应在长度在3-16个字符之间', trigger: 'blur' },
        ],
        loginPwd: [
          {required: true, message: '密码不能为空', trigger: 'blur'},
          { min: 3, max: 16, message: '密码应在长度在3-16个字符之间', trigger: 'blur' },
        ],
      },
      regRules: {
        userName: [
          {required: true, message: '用户名不能为空', trigger: 'blur'},
          { min: 3, max: 16, message: '用户名应在长度在3-16个字符之间', trigger: 'blur' },
        ],
        userPwd: [
          {required: true, message: '密码不能为空', trigger: 'blur'},
          { min: 3, max: 16, message: '密码应在长度在3-16个字符之间', trigger: 'blur' },
        ],
        checkPwd: [
          { validator: validatePwd, trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    doLogin() {
      this.$refs.loginForm.validate(async valid => {
        if (!valid) return;
        this.$axios.post('/user-info/verify', this.user).then(({data: res}) => {
          if (res.code === 0 && res.data !== null) {
            this.$router.push({path: '/index', query: {userName: res.data.userName}});
            this.$message.success(`登陆成功，欢迎[${res.data.userName}]回来！`)
          } else {
            this.$message.error(res.msg);
          }
        }).catch(err => {
          console.log(err)
          this.$message.error("网络繁忙，请稍后再试！")
        })
      })

    },
    doRegister() {
      this.regForm = {
        userName: '',
        userPwd: '',
        checkPwd: ''
      }
      this.regDialogFormVisible = true;
    },
    doResetPwd() {
      this.regForm = {
        userName: '',
        userPwd: '',
        checkPwd: ''
      }
      this.forgetDialogFormVisible = true;
    },
    submitReg() {
      this.$axios.put('/user-info/register', this.regForm).then(({data: res}) => {
        if (res.code === 0 && res.data > 0) {
          this.$router.push({path: '/index', query: {userName: this.regForm.userName}});
          this.$message.success(`注册成功，欢迎[${this.regForm.userName}]！`)
        } else {
          this.$message.error(res.msg)
        }
      }).catch(err => {
        console.log(err)
        this.$message.error("网络繁忙，请稍后再试！")
      })
    },
    forForget() {
      this.$axios.post('/user-info/resetPwd', this.regForm).then(({data: res}) => {
        if (res.code === 0) {
          if (res.data > 0) {
            this.$message.success('修改成功');
            this.user.loginPwd = '';
            this.user.loginName = '';
            this.forgetDialogFormVisible = false;
          } else {
            this.$message.warning("用户不存在");
          }
        } else {
          this.$message.error(res.msg)
        }
      }).catch(err => {
        console.log(err)
        this.$message.error("网络繁忙，请稍后再试！")
      })
    }
  }
}
</script>

<style scoped>
#loginContainer {
  width: 100%;
  height: 100%;
  background-color: whitesmoke;
  display: flex;
  align-items: center;
  justify-content: center;
  background-image: url("http://rpbz8r33e.hd-bkt.clouddn.com/default/18.jpg");
  background-repeat: no-repeat;
  background-size: 100%;
}

#loginContent h2 {
  width: 100%;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
}

#loginContent {
  width: 500px;
  padding: 20px 60px 20px 0;
  border: 1px gray solid;
  border-radius: 10px;
  margin-top: -60px;
  box-shadow: 0 0 30px gray;
  z-index: 1000;
  background-color: rgba(245, 245, 245, 0.7);
}

#aContent {
  width: 100%;
  text-align: right;
  margin-top: -65px;
}

#aContent a {
  text-decoration: none;
}

#btn {
  z-index: 1000;
}
</style>
